﻿using System.Security.Principal;
using System.Web;
using Repository.Abstract;
using Repository.NHibernate;
using Services.Abstract;
using Services.Concrete;
using StructureMap;

namespace IoC
{
    public class StructureMap
    {
        public static void Configure()
        {
            ObjectFactory.Initialize(r =>
            {
                r.Scan(a =>
                {
                    a.AssemblyContainingType(typeof(IRepository<>));
                    a.AssemblyContainingType(typeof(Repository<>));
                    a.WithDefaultConventions();
                });
                r.Scan(a =>
                {
                    a.AssemblyContainingType(typeof(IService<>));
                    a.AssemblyContainingType(typeof(Service<>));
                    a.WithDefaultConventions();
                });
                r.For<IAuthenticationService>().Use<FormsAuthenticationService>();
                r.For<IScheduler>().Use<QuartzScheduler>();
                r.For<IPrincipal>().Use(() => HttpContext.Current.User);
                r.For<IPermissionService>().Use<PermissionService>();
                r.SetAllProperties(p =>
                {
                    p.WithAnyTypeFromNamespace("Repository.Abstract");
                    p.WithAnyTypeFromNamespace("Services.Abstract");
                    p.OfType<IAuthenticationService>();
                });
            });
        }

        public static T Resolve<T>()
        {
            return ObjectFactory.GetInstance<T>();
        }

        public static void BuildUp(object target)
        {
            ObjectFactory.BuildUp(target);
        }
    }
}